package StudyCode.Concurrent.Unsynch;

/**
 * @author lixiaoyao
 */
public class UnsynchBankTest {

	private static final int MACCOUNTS = 100;
	private static final double INITIAL_BALANCE = 1000;
	private static final double MAX_AMOUNT = 1000;
	private static final int DELAY = 10;

	public static void main(String[] args) {
		final Bank bank = new Bank(MACCOUNTS, INITIAL_BALANCE);
		for (int i = 0; i < MACCOUNTS; i++) {
			final int fromAccount = i;
			final Runnable runnable = new Runnable() {
				@Override
				public void run() {
					try {
						while (true) {
							int toAccount = (int) (bank.size() * Math.random());
							double amount = MAX_AMOUNT * Math.random();
							bank.transfer(fromAccount,toAccount,amount);
							Thread.sleep((int)(DELAY*Math.random()));
						}
					}catch (InterruptedException ex){

					}
				}

			};
			Thread thread=new Thread(runnable);
			thread.start();
		}
	}

}
